﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;

namespace RTEObjectLink
{
    [CLSCompliant(false)]
    [Guid("ddc2e192-88cd-4bde-83b8-fc6c23946332")]
    public class OLFeatureReceiver : SPFeatureReceiver
    {
        
		
        /// <summary>
        /// Occurs after a Feature is activated. 
        /// </summary>
        /// <param name="properties">
        /// A Microsoft.SharePoint.SPFeatureReceiverProperties object that represents properties of the event handler.
        /// </param>
		public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                if (site != null)
                {
                    SPFile rte =
                        site.RootWeb.GetFile("_catalogs/masterpage/Editing Menu/RTE2ToolbarExtension.xml");
                    if (rte != null)
                    {
                        rte.CheckOut();
                        // grab the binary file data into a byte array 
                        byte[] fileBytes = rte.OpenBinary();
                        // create an XML Document to grab the required information
                        XmlDocument xmlDoc = new XmlDocument();
                        // parse those bytes into a memory stream
                        using (MemoryStream fileStream = new MemoryStream(fileBytes))
                        {
                            // load in the XML form data
                            xmlDoc.Load(fileStream);
                        }
                        XmlNode root = xmlDoc.DocumentElement;
                        if (root != null)
                        {
                            XmlNode node = root.SelectSingleNode("RTE2ToolbarExtraButton[@id='ObjectLink']");
                            if (node == null)
                            {
                                XmlNode newnode = xmlDoc.CreateElement("RTE2ToolbarExtraButton");
                                XmlAttribute id = xmlDoc.CreateAttribute("id");
                                id.Value = "ObjectLink";
                                newnode.Attributes.Append(id);
                                XmlAttribute src = xmlDoc.CreateAttribute("src");
                                src.Value = "rteobjectlink-server.js";
                                newnode.Attributes.Append(src);
                                root.AppendChild(newnode);
                                fileBytes = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);
                                rte.SaveBinary(fileBytes);
                                rte.Update();
                                rte.CheckIn("Updated with RTEObjectLink");
                                rte.Publish("Updated with RTEObjectLink");
                                rte.Approve("");
                            }
                        }
                    }
                }
            }
		}
		
		/// <summary>
        /// Occurs when a Feature is deactivating.
        /// </summary>
        /// <param name="properties">
        /// A Microsoft.SharePoint.SPFeatureReceiverProperties object that represents properties of the event handler.
        /// </param>
		public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
		{

		}
		
		/// <summary>
        /// Occurs after a Feature is installed.
        /// </summary>
        /// <param name="properties">
        /// A Microsoft.SharePoint.SPFeatureReceiverProperties object that represents properties of the event handler.
        /// </param>
		public override void FeatureInstalled(SPFeatureReceiverProperties properties)
		{

		}
		
		/// <summary>
        /// Occurs when a Feature is uninstalling.
        /// </summary>
        /// <param name="properties">
        /// A Microsoft.SharePoint.SPFeatureReceiverProperties object that represents properties of the event handler.
        /// </param>
		public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
		{

		}
    }
}
